package mobilesafe.guoshisp.com.xdtong.net;

import android.os.Handler;
import android.os.Message;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import mobilesafe.guoshisp.com.xdtong.entity.StudentInfo;
import mobilesafe.guoshisp.com.xdtong.model.Const;

/**
 * Description:发送httpPost网络线程类
 * User: Dream_Coder(chenchen_839@126.com)
 * Date: 2015-07-19
 * Time: 00:49
 */
public class ThreadHttpPost implements Runnable{

    private Handler handler;
    private String url;
    private String[] mData;
    private StringBuffer content = new StringBuffer();
    private StudentInfo studentInfo;
    private HandleNetCallBack mCallBack;
    public ThreadHttpPost(Handler handler,String url){
        this.handler = handler;
        this.url = url;
    }

    public ThreadHttpPost(String url, String[] data,HandleNetCallBack callBack){
        this.url = url;
        this.mData = data;
        this.mCallBack = callBack;
    }
    @Override
    public void run() {
        Message msg = Message.obtain();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        httpPost.addHeader("Accept", Const.ACCEPT);
        httpPost.addHeader("Accept-Encoding", Const.ACCEPT_ECODING);
        httpPost.addHeader("Accept-Language", Const.ACCEPT_LANGUAGE);
        httpPost.addHeader("Cookie", mData[0]);
        httpPost.addHeader("Host", Const.HOST);
        httpPost.addHeader("Referer", Const.REFERER);
        httpPost.addHeader("User-Agent", Const.USER_AGENT);
        paramList.add(new BasicNameValuePair("username",mData[1]));
        paramList.add(new BasicNameValuePair("password", mData[1]));
        paramList.add(new BasicNameValuePair("identity", Const.IDENTITY));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8));
            HttpResponse httpResponses = httpClient.execute(httpPost);
            System.out.println(httpResponses.getStatusLine().getStatusCode());
            if (httpResponses.getStatusLine().getStatusCode() == 200) {
                mCallBack.OnSuccess(httpResponses);
            }else {
                mCallBack.OnFail(httpResponses);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
